package cn.comtom.ewbs.signature.common.utils;


import org.springframework.util.Base64Utils;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.HexUtil;
import cn.hutool.crypto.asymmetric.SM2;

public class Sm2Test {
    public static String x = "E45FB09A87658BF8D92D0C1B391D953C66598AEFF8EEE61A88AD993F7F239565";
    public static String y = "1CCFED6465F5062778CFC396D4D9DFC44C2040B8B0E615916ED63B45465753E6";
    public static String privateKeyHex = "2FB22AEDD02205AF5B8F4171DC1DCBB5A4643D2BF0034CF7D0283CCAE193EF50";
    /**
     * 用于建立十六进制字符的输出的大写字符数组
     */
    private static final char[] DIGITS_UPPER = { '0', '1', '2', '3', '4', '5',
            '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
    public static String getSign(String dataHex){
// 数据和ID此处使用16进制表示
        final SM2 sm2 = new SM2(privateKeyHex, x, y);
// 生成的签名是64位
        sm2.usePlainEncoding();

        final String sign = sm2.signHex(dataHex);
        //Console.log(sign);
        return sign;
// true
    }

    public static boolean verfySign(String dataHex, String signHex){
// 数据和ID此处使用16进制表示

        final SM2 sm2 = new SM2(privateKeyHex, x, y);
// 生成的签名是64位
        sm2.usePlainEncoding();
// true
        boolean verify = sm2.verifyHex(dataHex, signHex);
        Console.log(verify);
        return verify;
    }
    public static  char[] encodeHex(byte[] data) {
        return encodeHex(data, DIGITS_UPPER);
    }

    public static  char[] encodeHex(byte[] data, char[] toDigits) {
        int l = data.length;
        char[] out = new char[l << 1];
        // two characters form the hex value.
        for (int i = 0, j = 0; i < l; i++) {
            out[j++] = toDigits[(0xF0 & data[i]) >>> 4];
            out[j++] = toDigits[0x0F & data[i]];
        }
        return out;
    }
    public static void main(String[] args) {

        byte[] bytes = FileUtil.readBytes("D:\\test\\EBDT_10245000000000001030101010000000000093170\\EBDB_10245000000000001030101010000000000093170.xml");
         //String dataHex = Base64Utils.encodeToString(bytes);

         String dataHex = HexUtil.encodeHexStr(bytes);
         //Console.log(dataHex);
        String signHexs = getSign(dataHex);
        Console.log(signHexs.toUpperCase());
        byte[] bytes1 = HexUtil.decodeHex(signHexs);
        String base64sign5 = "YQH+SAAAAAAAAFs7QLqWmhPyJtqhx4nxsuVbfQXk9/9ZAIrtPX4/KgmIu/iSYH/HS173KgnpOx6tcJpph/+y0KVdNdvrz6FVtbE=";
        Console.log(base64sign5);
        byte[] bytes2 = Base64Utils.decodeFromString(base64sign5);
        String signHex7 = HexUtil.encodeHexStr(bytes2);
        Console.log(signHex7);
        boolean b = verfySign(dataHex, signHex7.substring(20));
        //String dataHex ="434477813974bf58f94bcf760833c2b40f77a5fc360485b0b9ed1bd9682edb45";
        //String signHex = getSign(dataHex);
        /*String base64SignValue = "YQL4uQAAAAAAAWPrJO7l/bEYQqs2k8gZPi6rMZJ2bX9qFzQ4CGhDqj5W75HsWcFWfrvJFkTI5FhgvTfQxq4DNjmV1D8QhxU7NJk=";
        byte[] signbytes = Base64Utils.decodeFromString(base64SignValue);
        String signHex = new String(encodeHex(signbytes));

        //Console.log(signHex);
        boolean b = verfySign(dataHex, signHex.substring(20).toLowerCase());
        Console.log(signHex.substring(20));


        String signValue64 = DatatypeConverter.printHexBinary(DatatypeConverter.parseBase64Binary(base64SignValue));
        Console.log(signValue64);
        boolean b2 = verfySign(dataHex, signHex.substring(20));
        Console.log(signValue64.substring(20));
        Console.log(b2);*/
    }

    public static byte[] aschex_to_bcdhex(byte[] aschexByte) {
        int j = 0;
        if (aschexByte.length % 2 == 0) {
            j = aschexByte.length / 2;
            byte[] resTmp = new byte[j];
            for (int i = 0; i < j; i++) {
                resTmp[i] = ascToHex(aschexByte[2 * i], aschexByte[2 * i + 1]);
            }
            return resTmp;

        } else {
            j = aschexByte.length / 2 + 1;
            byte[] resTmp = new byte[j];
            for (int i = 0; i < j - 1; i++) {
                resTmp[i] = ascToHex((byte) aschexByte[2 * i],
                        (byte) aschexByte[2 * i + 1]);
            }
            resTmp[j - 1] = ascToHex((byte) aschexByte[2 * (j - 1)], (byte) 0);
            return resTmp;
        }
    }
    private static byte ascToHex(byte ch1, byte ch2) {
        byte ch;
        if (ch1 >= 'A')
            ch = (byte) ((ch1 - 0x37) << 4);
        else
            ch = (byte) ((ch1 - '0') << 4);
        if (ch2 >= 'A')
            ch |= (byte) (ch2 - 0x37);
        else
            ch |= (byte) (ch2 - '0');
        return ch;
    }

}
